/*  This file is part of the comicconquer project
    Copyright (C) 2010 Reach <reach334@gmail.com>

    ComicConquer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This software is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public 
    License along with this software.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "CoListDlg.h"

CoListDlg::CoListDlg(CoListItems* items, QWidget* parent)
: ui::OnyxDialog(parent)
{
    setFixedSize(300, 500);

    updateTitle(tr("列表"));

    QVBoxLayout* mainlayout = new QVBoxLayout(this);
    content_widget_.setLayout(mainlayout);

    m_list = new CoListWidget(items, this);
    mainlayout->addWidget(m_list);

    connect(m_list, SIGNAL(activated(const QModelIndex&)), this, SLOT(selectedItem(const QModelIndex&)));
}

CoListItem* CoListDlg::popup()
{
    if (exec() == Accepted)
    {
        return m_list->getListItem(m_selectId);
    }
    return NULL;
}

CoListItem* CoListDlg::select(CoListItems* items, QWidget* parent)
{
    CoListDlg dlg(items, parent);
    if (dlg.exec() == Accepted)
    {
        return dlg.m_list->getListItem(dlg.m_selectId);
    }
    return NULL;
}

void CoListDlg::selectedItem(const QModelIndex& index)
{
    m_selectId = index.row();
    accept();
}

bool CoListDlg::event(QEvent* e)
{
    bool ret = ui::OnyxDialog::event(e);

    switch (e->type())
    {
    case QEvent::UpdateRequest:
        {
            refreshScreen();
            return true;
        }
        break;
    default:
        {

        }
        break;
    }
    return ret;
}

void CoListDlg::refreshScreen(ScreenProxy::Waveform w, bool whole)
{
    ScreenProxy::instance().updateWidget(this, w, whole);
}

void CoListDlg::keyReleaseEvent(QKeyEvent* event)
{
    if (event->key() == Qt::Key_Escape)
    {
        reject();
    }
    else
    {
        m_list->keyReleaseEvent(event);
    }
}
